package com.ikangtai.shecare.home.circlecalendar;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ikangtai.shecare.R;
import com.ikangtai.shecare.common.App;
import com.ikangtai.shecare.common.al.DayUnitDSOutput;

import java.util.ArrayList;
import java.util.Locale;


public class PageAdapter extends PagerAdapter {
    private ArrayList<Calendar> calendars;
    private Context context;

    private int mCurrentPosition;
    private boolean mRightScroll = true;
    private Callback mCallback;

    public PageAdapter(Context context, ArrayList<Calendar> calendars, Callback callback) {
        this.context = context;
        this.calendars = calendars;
        mCallback = callback;

    }

    public interface Callback {
        public void click(View v);
    }

    @Override
    public int getCount() {
        return calendars.size();
    }


    @Override
    public Object instantiateItem(final ViewGroup container, final int position) {
        final View view = LayoutInflater.from(context).inflate(R.layout.item_view, null);

        view.setTag(position);
        TextView it_day_number = (TextView) view.findViewById(R.id.it_day_number);
        final TextView it_day_title = (TextView) view.findViewById(R.id.it_day_title);
        TextView it_day_state = (TextView) view.findViewById(R.id.it_day_state);
        RoundProgressBar day_circle_progress_bar = (RoundProgressBar) view.findViewById(R.id.day_circle_progress_bar);
        final LinearLayout it_day_linearLayout = (LinearLayout) view.findViewById(R.id.it_day_linearLayout);
        TextView it_day_day = (TextView) view.findViewById(R.id.it_day_day);

        TextView it_percent_number = (TextView) view.findViewById(R.id.it_percent_number);
        final TextView it_percent_title = (TextView) view.findViewById(R.id.it_percent_title);
        TextView it_percent_state = (TextView) view.findViewById(R.id.it_percent_state);
        RoundProgressBar percent_circle_progress_bar = (RoundProgressBar) view.findViewById(R.id.percent_circle_progress_bar);
        final LinearLayout it_percent_linearLayout = (LinearLayout) view.findViewById(R.id.it_percent_linearLayout);
        TextView it_day_percent = (TextView) view.findViewById(R.id.it_day_percent);

        switch (Locale.getDefault().getLanguage()) {
            case "zh":

                it_day_number.setText(calendars.get(position).getDayDay());
                break;
            default:
                it_day_number.setText(calendars.get(position).getDayDay() + "-");
        }
        it_day_title.setText(calendars.get(position).getDayTitle());
        it_day_state.setText(calendars.get(position).getState());

        it_percent_number.setText(calendars.get(position).getPercentNumber());
        it_percent_title.setText(calendars.get(position).getPercentTitle());
        it_percent_state.setText(calendars.get(position).getState());

        it_day_day.setText(calendars.get(position).getDay());
        it_day_percent.setText(calendars.get(position).getDay());
        day_circle_progress_bar.setMax(100);
        percent_circle_progress_bar.setMax(100);
        day_circle_progress_bar.setProgress(Math.round(Float.parseFloat(calendars.get(position).getPercentNumberWithoutDay())));
        percent_circle_progress_bar.setProgress(Math.round(Float.parseFloat(calendars.get(position).getPercentNumberWithoutDay())));
        switch (Locale.getDefault().getLanguage()) {
            case "zh":
                switch (calendars.get(position).getState()) {

                    case "黄体期":
                        percent_circle_progress_bar.setCricleProgressColor(R.color.circle_bg_yellow_d);
                        day_circle_progress_bar.setCricleProgressColor(R.color.circle_bg_yellow_d);
                        percent_circle_progress_bar.setCricleColor(R.color.circle_bg_yellow);
                        day_circle_progress_bar.setCricleColor(R.color.circle_bg_yellow);
                        it_day_linearLayout.setBackgroundResource(R.drawable.circle_bg_yellow);
                        it_percent_linearLayout.setBackgroundResource(R.drawable.circle_bg_yellow);
                        it_percent_state.setBackgroundResource(R.drawable.circle_bt_bg_yellow);
                        it_day_state.setBackgroundResource(R.drawable.circle_bt_bg_yellow);
                        it_day_title.setTextColor(context.getResources().getColor(R.color.circle_bg_yellow_d));
                        it_percent_title.setTextColor(context.getResources().getColor(R.color.circle_bg_yellow_d));
                        break;
                    case "月经期":
                        if (App.dayUnitDSOutputsList.get(position).isConfrimOrForecast(App.status) == DayUnitDSOutput.PERIOD_CONFIRM_YJQ) {
                            percent_circle_progress_bar.setCricleProgressColor(R.color.circle_bg_pink_d);
                            day_circle_progress_bar.setCricleProgressColor(R.color.circle_bg_pink_d);
                            percent_circle_progress_bar.setCricleColor(R.color.circle_bg_pink);
                            day_circle_progress_bar.setCricleColor(R.color.circle_bg_pink);

                            it_day_linearLayout.setBackgroundResource(R.drawable.circle_bg_pink);
                            it_percent_linearLayout.setBackgroundResource(R.drawable.circle_bg_pink);
                            it_percent_state.setBackgroundResource(R.drawable.circle_bt_bg_pink);
                            it_day_state.setBackgroundResource(R.drawable.circle_bt_bg_pink);
                            it_day_title.setTextColor(context.getResources().getColor(R.color.circle_bg_pink_d));
                            it_percent_title.setTextColor(context.getResources().getColor(R.color.circle_bg_pink_d));
                        } else {
                            percent_circle_progress_bar.setCricleProgressColor(R.color.circle_bg_pink_d_pre);
                            day_circle_progress_bar.setCricleProgressColor(R.color.circle_bg_pink_d_pre);
                            percent_circle_progress_bar.setCricleColor(R.color.circle_bg_pink_pre);
                            day_circle_progress_bar.setCricleColor(R.color.circle_bg_pink_pre);

                            it_day_linearLayout.setBackgroundResource(R.drawable.circle_bg_pink_pre);
                            it_percent_linearLayout.setBackgroundResource(R.drawable.circle_bg_pink_pre);
                            it_percent_state.setBackgroundResource(R.drawable.circle_bt_bg_pink_pre);
                            it_day_state.setBackgroundResource(R.drawable.circle_bt_bg_pink_pre);
                            it_day_title.setTextColor(context.getResources().getColor(R.color.circle_bg_pink_d_pre));
                            it_percent_title.setTextColor(context.getResources().getColor(R.color.circle_bg_pink_d_pre));
                        }
                        break;
                    case "卵泡期":
                        percent_circle_progress_bar.setCricleProgressColor(R.color.circle_bg_blue_d);
                        day_circle_progress_bar.setCricleProgressColor(R.color.circle_bg_blue_d);
                        percent_circle_progress_bar.setCricleColor(R.color.circle_bg_blue);
                        day_circle_progress_bar.setCricleColor(R.color.circle_bg_blue);

                        it_day_linearLayout.setBackgroundResource(R.drawable.circle_bg_blue);
                        it_percent_linearLayout.setBackgroundResource(R.drawable.circle_bg_blue);
                        it_percent_state.setBackgroundResource(R.drawable.circle_bt_bg_blue);
                        it_day_state.setBackgroundResource(R.drawable.circle_bt_bg_blue);
                        it_day_title.setTextColor(context.getResources().getColor(R.color.circle_bg_blue_d));
                        it_percent_title.setTextColor(context.getResources().getColor(R.color.circle_bg_blue_d));
                        break;
                    case "安全期":
                        percent_circle_progress_bar.setCricleProgressColor(R.color.circle_bg_blue_d);
                        day_circle_progress_bar.setCricleProgressColor(R.color.circle_bg_blue_d);
                        percent_circle_progress_bar.setCricleColor(R.color.circle_bg_blue);
                        day_circle_progress_bar.setCricleColor(R.color.circle_bg_blue);

                        it_day_linearLayout.setBackgroundResource(R.drawable.circle_bg_blue);
                        it_percent_linearLayout.setBackgroundResource(R.drawable.circle_bg_blue);
                        it_percent_state.setBackgroundResource(R.drawable.circle_bt_bg_blue);
                        it_day_state.setBackgroundResource(R.drawable.circle_bt_bg_blue);
                        it_day_title.setTextColor(context.getResources().getColor(R.color.circle_bg_blue_d));
                        it_percent_title.setTextColor(context.getResources().getColor(R.color.circle_bg_blue_d));
                        break;
                    case "易孕期":
                        percent_circle_progress_bar.setCricleProgressColor(R.color.circle_bg_red_d);
                        day_circle_progress_bar.setCricleProgressColor(R.color.circle_bg_red_d);
                        percent_circle_progress_bar.setCricleColor(R.color.circle_bg_red);
                        day_circle_progress_bar.setCricleColor(R.color.circle_bg_red);

                        it_day_linearLayout.setBackgroundResource(R.drawable.circle_bg_red);
                        it_percent_linearLayout.setBackgroundResource(R.drawable.circle_bg_red);
                        it_percent_state.setBackgroundResource(R.drawable.circle_bt_bg_red);
                        it_day_state.setBackgroundResource(R.drawable.circle_bt_bg_red);
                        it_day_title.setTextColor(context.getResources().getColor(R.color.circle_bg_red_d));
                        it_percent_title.setTextColor(context.getResources().getColor(R.color.circle_bg_red_d));
                        break;
                    default:
                        percent_circle_progress_bar.setCricleProgressColor(R.color.circle_bg_green_d);
                        day_circle_progress_bar.setCricleProgressColor(R.color.circle_bg_green_d);
                        percent_circle_progress_bar.setCricleColor(R.color.circle_bg_green);
                        day_circle_progress_bar.setCricleColor(R.color.circle_bg_green);

                        it_day_linearLayout.setBackgroundResource(R.drawable.circle_bg_green);
                        it_percent_linearLayout.setBackgroundResource(R.drawable.circle_bg_green);
                        it_percent_state.setBackgroundResource(R.drawable.circle_bt_bg_green);
                        if (App.status == 1) {
                            it_day_state.setText("安全期");
                        }
                        it_day_state.setBackgroundResource(R.drawable.circle_bt_bg_green);
                        it_day_title.setTextColor(context.getResources().getColor(R.color.circle_bg_green_d));
                        it_percent_title.setTextColor(context.getResources().getColor(R.color.circle_bg_green_d));
                        break;
                }

                break;
            default:
                switch (calendars.get(position).getState()) {
                    case "Luteal Phase":
                        percent_circle_progress_bar.setCricleProgressColor(R.color.circle_bg_yellow_d);
                        day_circle_progress_bar.setCricleProgressColor(R.color.circle_bg_yellow_d);
                        percent_circle_progress_bar.setCricleColor(R.color.circle_bg_yellow);
                        day_circle_progress_bar.setCricleColor(R.color.circle_bg_yellow);
                        it_day_linearLayout.setBackgroundResource(R.drawable.circle_bg_yellow);
                        it_percent_linearLayout.setBackgroundResource(R.drawable.circle_bg_yellow);
                        it_percent_state.setBackgroundResource(R.drawable.circle_bt_bg_yellow);
                        it_day_state.setBackgroundResource(R.drawable.circle_bt_bg_yellow);
                        it_day_title.setTextColor(context.getResources().getColor(R.color.circle_bg_yellow_d));
                        it_percent_title.setTextColor(context.getResources().getColor(R.color.circle_bg_yellow_d));
                        break;
                    case "Menstruation":
                        percent_circle_progress_bar.setCricleProgressColor(R.color.circle_bg_pink_d);
                        day_circle_progress_bar.setCricleProgressColor(R.color.circle_bg_pink_d);
                        percent_circle_progress_bar.setCricleColor(R.color.circle_bg_pink);
                        day_circle_progress_bar.setCricleColor(R.color.circle_bg_pink);

                        it_day_linearLayout.setBackgroundResource(R.drawable.circle_bg_pink);
                        it_percent_linearLayout.setBackgroundResource(R.drawable.circle_bg_pink);
                        it_percent_state.setBackgroundResource(R.drawable.circle_bt_bg_pink);
                        it_day_state.setBackgroundResource(R.drawable.circle_bt_bg_pink);
                        it_day_title.setTextColor(context.getResources().getColor(R.color.circle_bg_pink_d));
                        it_percent_title.setTextColor(context.getResources().getColor(R.color.circle_bg_pink_d));
                        break;
                    case "Follicular Phase":
                        percent_circle_progress_bar.setCricleProgressColor(R.color.circle_bg_blue_d);
                        day_circle_progress_bar.setCricleProgressColor(R.color.circle_bg_blue_d);
                        percent_circle_progress_bar.setCricleColor(R.color.circle_bg_blue);
                        day_circle_progress_bar.setCricleColor(R.color.circle_bg_blue);

                        it_day_linearLayout.setBackgroundResource(R.drawable.circle_bg_blue);
                        it_percent_linearLayout.setBackgroundResource(R.drawable.circle_bg_blue);
                        it_percent_state.setBackgroundResource(R.drawable.circle_bt_bg_blue);
                        it_day_state.setBackgroundResource(R.drawable.circle_bt_bg_blue);
                        it_day_title.setTextColor(context.getResources().getColor(R.color.circle_bg_blue_d));
                        it_percent_title.setTextColor(context.getResources().getColor(R.color.circle_bg_blue_d));
                        break;
                    case "Infertile Day":
                        percent_circle_progress_bar.setCricleProgressColor(R.color.circle_bg_bottel_green_d);
                        day_circle_progress_bar.setCricleProgressColor(R.color.circle_bg_bottel_green_d);
                        percent_circle_progress_bar.setCricleColor(R.color.circle_bg_bottel_green);
                        day_circle_progress_bar.setCricleColor(R.color.circle_bg_bottel_green);

                        it_day_linearLayout.setBackgroundResource(R.drawable.circle_bg_bottel_green);
                        it_percent_linearLayout.setBackgroundResource(R.drawable.circle_bg_bottel_green);
                        it_percent_state.setBackgroundResource(R.drawable.circle_bt_bg_bottel_green);
                        it_day_state.setBackgroundResource(R.drawable.circle_bt_bg_bottel_green);
                        it_day_title.setTextColor(context.getResources().getColor(R.color.circle_bg_bottel_green_d));
                        it_percent_title.setTextColor(context.getResources().getColor(R.color.circle_bg_bottel_green_d));
                        break;
                    case "Fertile Day":
                        percent_circle_progress_bar.setCricleProgressColor(R.color.circle_bg_red_d);
                        day_circle_progress_bar.setCricleProgressColor(R.color.circle_bg_red_d);
                        percent_circle_progress_bar.setCricleColor(R.color.circle_bg_red);
                        day_circle_progress_bar.setCricleColor(R.color.circle_bg_red);

                        it_day_linearLayout.setBackgroundResource(R.drawable.circle_bg_red);
                        it_percent_linearLayout.setBackgroundResource(R.drawable.circle_bg_red);
                        it_percent_state.setBackgroundResource(R.drawable.circle_bt_bg_red);
                        it_day_state.setBackgroundResource(R.drawable.circle_bt_bg_red);
                        it_day_title.setTextColor(context.getResources().getColor(R.color.circle_bg_red_d));
                        it_percent_title.setTextColor(context.getResources().getColor(R.color.circle_bg_red_d));
                        break;
                    default:
                        percent_circle_progress_bar.setCricleProgressColor(R.color.circle_bg_green_d);
                        day_circle_progress_bar.setCricleProgressColor(R.color.circle_bg_green_d);
                        percent_circle_progress_bar.setCricleColor(R.color.circle_bg_green);
                        day_circle_progress_bar.setCricleColor(R.color.circle_bg_green);

                        it_day_linearLayout.setBackgroundResource(R.drawable.circle_bg_green);
                        it_percent_linearLayout.setBackgroundResource(R.drawable.circle_bg_green);
                        it_percent_state.setBackgroundResource(R.drawable.circle_bt_bg_green);
                        if (App.status == 1) {
                            it_day_state.setText("Infertile Day");
                        }
                        it_day_state.setBackgroundResource(R.drawable.circle_bt_bg_green);
                        it_day_title.setTextColor(context.getResources().getColor(R.color.circle_bg_green_d));
                        it_percent_title.setTextColor(context.getResources().getColor(R.color.circle_bg_green_d));
                        break;
                }
        }


        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCallback.click(v);
            }
        });

        container.addView(view);
        return view;
    }


    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View view = (View) object;
        container.removeView(view);
    }

    @Override
    public float getPageWidth(int position) {
        return (float) 0.2;
    }

    private View mCurrentView;

    @Override
    public void setPrimaryItem(ViewGroup container, final int position, Object object) {
        mCurrentView = (View) object;
        mCurrentPosition = position;
    }

    public View getPrimaryItem() {
        return mCurrentView;
    }

    public int getPrimaryItemPostion() {
        return mCurrentPosition;
    }

    public boolean getRightScroll() {
        return mRightScroll;
    }


}
